(() => {
	let ele = {};

	ele.payee = $('#payee');
	ele.alipay = $('#alipay');
	ele.confirm = $('#confirm');

	init();
	function init() {
		let alipay_name = getCookie('alipay_name');
		let alipay_account = getCookie('alipay_account');
		if (alipay_name != 'null' && alipay_name != '') {
			ele.payee.value = alipay_name;
			ele.alipay.value = alipay_account;
			ele.payee.disabled = 'disabled';
			ele.alipay.disabled = 'disabled';
			ele.confirm.disabled = 'disabled';
			ele.confirm.innerHTML = '<span class="border">' + '绑定成功' + '</span>';
		} else {
			ele.confirm.onclick = () => {
				let payee = ele.payee.value;
				let alipay = ele.alipay.value;
				if (!payee) openTips('收款人姓名不能为空');
				// else if (!/^[\u4e00-\u9fa5]{2,4}$/.test(payee)) openTips('收款人姓名不合法');
				else if (payee.length < 2 || payee.length > 12) openTips('收款人姓名不合法');
				else if (!alipay) openTips('支付宝账号不能为空');
				else if (!/(^1[3-9]\d{9}$)|(^[\w\d-]+(\.[\w\d-]+)*@[\w\d-]+(\.[\w\d-]+)+$)/.test(alipay))
					openTips('支付宝账号不合法');
				else bindAccount(payee, alipay);

			}
		}
	}

	function bindAccount(name, account) {
		//请求绑定支付宝接口
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.BIND_ALIPAY,
			method: 'POST',
			header: {
				Authorization: token
			},
			data: {
				alipay_name: name,
				alipay_account: account
			},
			success: res => {
				if (res.code == 1) {
					let dt = 365 * 24 * 60 * 60;
					setCookie('alipay_name', name, dt);
					setCookie('alipay_account', account, dt);
					init();
				}
				openTips(res.msg || '意外的错误');
			}
		});
	}
})();
